﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.Models
{
    public class TrayCalculatorCary
    {
        private readonly PointCary[] corners;
        private const int Rows = 8;
        private const int Columns = 6;
        //离心管瓶盖点位   
        //135.618,500.763,397.801,-136.732,-1.86878,89.74   右下 
        //-38.8809,506.329,399.227,-136.731,-1.85918,89.7366   右上 
        //-41.5535,398.519,399.413,-136.729,-1.8481,89.7413    左上  
        //133.442,393.266,400.437,-136.728,-1.84387,89.7411   左下
        //127.623,394.477
        public TrayCalculatorCary()
        {
            corners = new PointCary[4] {
            new PointCary(127.583f,394.275f), // 左下角
            new PointCary(130.551f,501.981f), // 右下角
            new PointCary(-44.5749f,506.251f), // 右上角
            new PointCary(-47.3018f,398.61f) // 左上角
        };
        }

        public PointCary GetPosition(int positionNumber)
        {
            if (positionNumber < 1 || positionNumber > Rows * Columns)
                throw new ArgumentOutOfRangeException("点位编号必须在1-32之间");

            // 计算行列索引(0-based)
            int col = (positionNumber - 1) / Rows;  // 列优先计算
            int row = (positionNumber - 1) % Rows;

            // Z字形排列：奇数列从下到上，偶数列从上到下
            if (col % 2 == 1)
            {
                row = Rows - 1 - row;
            }

            // 计算插值比例
            float xRatio = (float)col / (Columns - 1);
            float yRatio = (float)row / (Rows - 1);

            // 双线性插值计算坐标
            PointCary bottom = Lerp(corners[0], corners[1], xRatio);
            PointCary top = Lerp(corners[3], corners[2], xRatio);
            return Lerp(bottom, top, yRatio);
        }

        private PointCary Lerp(PointCary a, PointCary b, float t)
        {
            return new PointCary(
                a.X + (b.X - a.X) * t,
                a.Y + (b.Y - a.Y) * t);
        }
    }

    public struct PointCary
    {
        public float X { get; set; }
        public float Y { get; set; }

        public PointCary(float x, float y)
        {
            X = x;
            Y = y;
        }
    }





}
